<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2024/1/8
  Time: 14:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <%--${list}--%>
    <form class="form-inline" action="/user?method=selectAll" method="post">
        <div class="form-group">
            <label>用户名</label>
            <input type="text" class="form-control"  name="name"/>
        </div>
        <input class="btn btn-primary" type="submit" value="搜索"/>
        <a class="btn btn-sm btn-primary" href="/user_add.jsp">添加</a>
        <a class="btn btn-sm btn-danger" href="javascript:deleteAll()">批量删除</a>
    </form>

    <table class="table table-striped table-bordered table-hover table-condensed">
        <tr>
            <td><input type="checkbox" id="checkAll"/></td>
            <td>ID</td>
            <td>名字</td>
            <td>密码</td>
            <td>邮箱</td>
            <td>头像</td>
            <td>角色</td>
            <td>状态</td>
            <td>删除</td>
            <td>编辑</td>
        </tr>
        <c:forEach items="${list}" var="user">
            <tr>
                <td><input type="checkbox" value="${user.id}" class="cb"/></td>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.password}</td>
                <td>${user.email}</td>
                <td>
                    <img src="/pic/${user.image}" width="30px" height="20px"/>
                </td>
                <td>
                    <c:choose>
                        <c:when test="${user.role==1}">
                            <span class="label label-warning">超级管理员</span>
                        </c:when>
                        <c:when test="${user.role==2}">
                            <span class="label label-success">普通管理员</span>
                        </c:when>
                        <c:otherwise>未知</c:otherwise>
                    </c:choose>
                </td>
                <td>
                    <c:choose>
                        <c:when test="${user.status==1}">
                            <span class="label label-warning">正常</span>
                        </c:when>
                        <c:when test="${user.status==0}">
                            <span class="label label-success">禁用</span>
                        </c:when>
                    </c:choose>
                </td>
                <td><a class="btn btn-xs btn-danger" href="javascript:deleteById(${user.id})">删除</a></td>
                <td><a class="btn btn-xs btn-primary" href="/user?method=toUpdate&id=${user.id}">编辑</a></td>
            </tr>
        </c:forEach>

    </table>

    <script>
        $(function (){
            $('#checkAll').click(function (){
                $('.cb').prop('checked', this.checked);
            });
        })

        function deleteAll() {
            layer.confirm('您确认要删除么？', function() {
                let array = new Array();
                //遍历所有加了class="cb"的checkbox，把value值放到数组
                $('.cb:checked').each(function() {
                    array.push($(this).val());
                })

                location.href = '/user?method=deleteAll&ids=' + array;
            });
        }

        function deleteById(id) {
            // let isDelete = confirm('您确认要删除么')
            // if (isDelete) {
            //     location.href = '/user?method=deleteById&id=' + id;
            // }


            layer.confirm('您确认要删除么？', function() {
                location.href = '/user?method=deleteById&id=' + id;
            });


        }
    </script>
</body>
</html>
